﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;


namespace TestDxfDocument
{
    class GB_Z_29014_301_10
    {
        public static void Outside_301_10(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + 8.5, v1.Y);
            Vector2 v3 = fun.Polar(v2, 60 * pi / 180, 1.73);
            Vector2 v4 = new Vector2(v3.X, v3.Y + 25);
            Vector2 v5 = fun.Polar(v4, 135 * pi / 180, 2.83);
            Vector2 v6 = new Vector2(v5.X, v5.Y + 10);
            Vector2 v7 = fun.Polar(v6, 60 * pi / 180, 5);
            Vector2 v8 = new Vector2(v7.X, v7.Y + 55);
            Vector2 v9 = new Vector2(-v8.X, v8.Y);
            Vector2 v10 = new Vector2(-v7.X, v7.Y);
            Vector2 v11 = new Vector2(-v6.X, v6.Y);
            Vector2 v12 = new Vector2(-v5.X, v5.Y);
            Vector2 v13 = new Vector2(-v4.X, v4.Y);
            Vector2 v14 = new Vector2(-v3.X, v3.Y);
            Vector2 v15 = new Vector2(-v2.X, v2.Y);
            Vector2 v16 = new Vector2(v9.X + 2.5, v9.Y);
            Vector2 v17 = new Vector2(v16.X + 5, v16.Y);
            Vector2 v18 = new Vector2(v16.X, v16.Y - 11);
            Vector2 v19 = new Vector2(v17.X, v17.Y - 11);
            Vector2 v20 = new Vector2(-v17.X, v17.Y);
            Vector2 v21 = new Vector2(-v16.X, v16.Y);
            Vector2 v22 = new Vector2(-v19.X, v19.Y);
            Vector2 v23 = new Vector2(-v18.X, v18.Y);
            Vector2 v24 = new Vector2(v13.X + 4.5, v13.Y);
            Vector2 v25 = new Vector2(v13.X, v13.Y - 6);
            Vector2 v26 = new Vector2(v4.X - 6, v4.Y);
            Vector2 v27 = new Vector2(v25.X, v25.Y - 11.25);
            Vector2 v28 = new Vector2(v4.X, v4.Y - 12.5);
            Vector2 v29 = new Vector2(v15.X + 7.28, v15.Y);
            Vector2 v30 = new Vector2(v28.X, v28.Y - 6);
            Vector2 v31 = new Vector2(v1.X + 3.3, v1.Y);

            Vector2 v32 = new Vector2(v15.X - 0.11, v15.Y + 40.9);

            Vector2 v33 = new Vector2(v1.X, v1.Y - 5);
            Vector2 v34 = new Vector2(v1.X, v1.Y + 103.06);

            //作图
            LwPolyline line1 = new LwPolyline();
            line1.Vertexes.Add(new LwPolylineVertex(v1));
            line1.Vertexes.Add(new LwPolylineVertex(v2));
            line1.Vertexes.Add(new LwPolylineVertex(v3));
            line1.Vertexes.Add(new LwPolylineVertex(v4));
            line1.Vertexes.Add(new LwPolylineVertex(v5));
            line1.Vertexes.Add(new LwPolylineVertex(v6));
            line1.Vertexes.Add(new LwPolylineVertex(v7));
            line1.Vertexes.Add(new LwPolylineVertex(v8));
            line1.Vertexes.Add(new LwPolylineVertex(v9));
            line1.Vertexes.Add(new LwPolylineVertex(v10));
            line1.Vertexes.Add(new LwPolylineVertex(v11));
            line1.Vertexes.Add(new LwPolylineVertex(v12));
            line1.Vertexes.Add(new LwPolylineVertex(v13));
            line1.Vertexes.Add(new LwPolylineVertex(v14));
            line1.Vertexes.Add(new LwPolylineVertex(v15));
            line1.Vertexes.Add(new LwPolylineVertex(v1));
            line1.Vertexes[3].Bulge = Math.Tan(-90 * pi / 180 / 4);
            line1.Vertexes[11].Bulge = Math.Tan(-90 * pi / 180 / 4);
            line1.Layer = contour;
            line1.Lineweight = Lineweight.W50;
            dxf.AddEntity(line1);
            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Layer = contour;
            line2.Lineweight = Lineweight.W50;
            dxf.AddEntity(line2);
            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v11));
            line3.Vertexes.Add(new LwPolylineVertex(v6));
            line3.Layer = contour;
            line3.Lineweight = Lineweight.W50;
            dxf.AddEntity(line3);
            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes.Add(new LwPolylineVertex(v4));
            line4.Layer = contour;
            line4.Lineweight = Lineweight.W50;
            dxf.AddEntity(line4);
            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v24));
            line5.Vertexes.Add(new LwPolylineVertex(v25));
            line5.Layer = contour;
            line5.Lineweight = Lineweight.W50;
            dxf.AddEntity(line5);
            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v26));
            line6.Vertexes.Add(new LwPolylineVertex(v27));
            line6.Layer = contour;
            line6.Lineweight = Lineweight.W50;
            dxf.AddEntity(line6);
            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v4));
            line7.Vertexes.Add(new LwPolylineVertex(v14));
            line7.Layer = contour;
            line7.Lineweight = Lineweight.W50;
            dxf.AddEntity(line7);
            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v28));
            line8.Vertexes.Add(new LwPolylineVertex(v29));
            line8.Layer = contour;
            line8.Lineweight = Lineweight.W50;
            dxf.AddEntity(line8);
            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v30));
            line9.Vertexes.Add(new LwPolylineVertex(v31));
            line9.Layer = contour;
            line9.Lineweight = Lineweight.W50;
            dxf.AddEntity(line9);
            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v16));
            line10.Vertexes.Add(new LwPolylineVertex(v18));
            line10.Vertexes.Add(new LwPolylineVertex(v19));
            line10.Vertexes.Add(new LwPolylineVertex(v17));
            line10.Layer = contour;
            line10.Lineweight = Lineweight.W50;
            dxf.AddEntity(line10);
            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v20));
            line11.Vertexes.Add(new LwPolylineVertex(v22));
            line11.Vertexes.Add(new LwPolylineVertex(v23));
            line11.Vertexes.Add(new LwPolylineVertex(v21));
            line11.Layer = contour;
            line11.Lineweight = Lineweight.W50;
            dxf.AddEntity(line11);
            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v16));
            line12.Vertexes.Add(new LwPolylineVertex(v19));
            line12.Layer = contour;
            line12.Lineweight = Lineweight.W50;
            dxf.AddEntity(line12);
            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v17));
            line13.Vertexes.Add(new LwPolylineVertex(v18));
            line13.Layer = contour;
            line13.Lineweight = Lineweight.W50;
            dxf.AddEntity(line13);
            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(v20));
            line14.Vertexes.Add(new LwPolylineVertex(v23));
            line14.Layer = contour;
            line14.Lineweight = Lineweight.W50;
            dxf.AddEntity(line14);
            LwPolyline line15 = new LwPolyline();
            line15.Vertexes.Add(new LwPolylineVertex(v21));
            line15.Vertexes.Add(new LwPolylineVertex(v22));
            line15.Layer = contour;
            line15.Lineweight = Lineweight.W50;
            dxf.AddEntity(line15);
            LwPolyline line16 = new LwPolyline();
            line16.Vertexes.Add(new LwPolylineVertex(v33));
            line16.Vertexes.Add(new LwPolylineVertex(v34));
            line16.Layer = centre;
            line16.Lineweight = Lineweight.W50;
            dxf.AddEntity(line16);

            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v24));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 190, 6, hline1, harc1, hatching);

            LwPolyline hatchpoly2 = new LwPolyline();
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v4));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v14));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v27));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly2.IsClosed = true;
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            Arc[] harc2 = new Arc[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
                if (htype2[i] == "Arc")
                {
                    harc2[i] = (Arc)hatchpoly2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype2, hnumst2, 190, 6, hline2, harc2, hatching);

            LwPolyline hatchpoly3 = new LwPolyline();
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v29));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v31));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v30));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly3.IsClosed = true;
            String[] htype3 = new String[100];
            Line[] hline3 = new Line[100];
            Arc[] harc3 = new Arc[100];
            int hnumst3 = hatchpoly3.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype3[i] = hatchpoly3.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype3[i] == "Line")
                {
                    hline3[i] = (Line)hatchpoly3.Explode()[i];
                }
                if (htype3[i] == "Arc")
                {
                    harc3[i] = (Arc)hatchpoly3.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype3, hnumst3, 190, 6, hline3, harc3, hatching);

            //标注
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.5;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 1;
            double offset = 10;

            LinearDimension dim1 = new LinearDimension(new Line(v8, v9), offset * 1, 0, dimensionStyle);
            dim1.UserText = "DMM";
            dxf.AddEntity(dim1);
            LinearDimension dim2 = new LinearDimension(new Line(v9, v15), offset * 2, 90, dimensionStyle);
            dim2.UserText = "OAL";
            dxf.AddEntity(dim2);
            LinearDimension dim3 = new LinearDimension(new Line(v13, v14), offset * 0.8, 90, dimensionStyle);
            dim3.UserText = "THL";
            dxf.AddEntity(dim3);
            LinearDimension dim4 = new LinearDimension(new Line(v15, v32), offset * 1.5, 90, dimensionStyle);
            dim4.UserText = "LU";
            dxf.AddEntity(dim4);
            LinearDimension dim5 = new LinearDimension(new Line(v10, v9), offset * 1, 90, dimensionStyle);
            dim5.UserText = "LS";
            dxf.AddEntity(dim5);
            LinearDimension dim6 = new LinearDimension(new Line(v2, v3), offset * 1, 270, dimensionStyle);
            dim6.UserText = "TCPL";
            dxf.AddEntity(dim6);
            LinearDimension dim7 = new LinearDimension(new Line(v15, v2), offset * -1, 0, dimensionStyle);
            dim7.UserText = "TCPD";
            dxf.AddEntity(dim7);
            LinearDimension dim8 = new LinearDimension(new Line(v14, v3), offset * -2, 0, dimensionStyle);
            dim8.UserText = "TD";
            dxf.AddEntity(dim8);
            Angular2LineDimension angdim1 = new Angular2LineDimension(new Line(new Vector2(v4.X - 9.37, v4.Y - 12.5), new Vector2(v4.X, v4.Y)), new Line(new Vector2(v4.X - 9.37, v4.Y - 12.5), new Vector2(v4.X - 9.37, v4.Y)), offset * 6, dimensionStyle);
            angdim1.UserText = "FHA";//角度标注
            dxf.AddEntity(angdim1);



            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
